import wx
import os


class MainWindow(wx.Frame):
    def __init__(self,parent,title):
        wx.Frame.__init__(self,parent,title=title,size=(800,600))
        self.control=wx.TextCtrl(self,style=wx.TE_MULTILINE)

        self.CreateStatusBar()

        # menu1
        menu=wx.Menu()
        aboutItem=menu.Append(wx.ID_ABOUT,"About","Information")
        self.Bind(wx.EVT_MENU,self.onAbout,aboutItem)

        menu.AppendSeparator()
        exitItem=menu.Append(wx.ID_EXIT,"Exit","Terminate the program")
        self.Bind(wx.EVT_MENU,self.onExit,exitItem)

        menu.AppendSeparator()
        fileItem=menu.Append(wx.ID_FILE,"open","open file")
        self.Bind(wx.EVT_MENU,self.onOpen,fileItem)

        # menu2
        menu2=wx.Menu()

        #menuBar
        menuBar=wx.MenuBar()
        menuBar.Append(menu,"action")
        menuBar.Append(menu2,"more")
        self.SetMenuBar(menuBar)


        self.sizer1=wx.BoxSizer(wx.HORIZONTAL)
        self.buttons=[]
        for i in range(0,6):
            self.buttons.append(wx.Button(self,-1,"btn"+str(i)))
            self.buttons[i].Bind(wx.EVT_BUTTON,self.onClick)
            self.sizer1.Add(self.buttons[i],i,wx.EXPAND)
        self.rootLayout = wx.BoxSizer(wx.VERTICAL)

        self.sizer2=wx.BoxSizer(wx.HORIZONTAL)
        self.textview=wx.StaticText(self,-1,"你好",size=(100,100))
        self.sizer2.Add(self.textview,wx.EXPAND);

        #Add 方法参数2相当于权重
        self.rootLayout.Add(self.control, 1, wx.EXPAND)
        self.rootLayout.Add(self.sizer1,2, wx.EXPAND)
        self.rootLayout.Add(self.sizer2,1,wx.ALIGN_CENTER_HORIZONTAL)

        self.SetSizer(self.rootLayout)
        self.SetAutoLayout(1)
        self.rootLayout.Fit(self)
        self.Show(True)


    def onAbout(self,event):
        print(event)
        wx.MessageDialog(self,"hello","点击",wx.OK)
    def onExit(self,event):
        print("exit click")
        self.Close(True)
    def onOpen(self,e):
        self.dirname=''
        dlg=wx.FileDialog(self,"选择文件",self.dirname,"","*.*",wx.FD_OPEN)
        if dlg.ShowModal()==wx.ID_OK:
            print("选择文件:"+dlg.GetDirectory()+"\\"+dlg.GetFilename())
            self.filename=dlg.GetFilename()
            self.dirname=dlg.GetDirectory()
            f=open(os.path.join(self.dirname,self.filename,'r'))
            f.close()
        dlg.Destroy()
    def onClick(self,e):
        btn = e.GetEventObject().GetLabel()
        print(btn)

app = wx.App()
frame =MainWindow(None,  "Hello, World!")
frame.Show(True)
app.MainLoop()